import { faker } from '@faker-js/faker';
import { verifyAccessToken } from '~/utils/jwt-utils';
import { unAuthorizedResponse, useResponseSuccess } from '~/utils/response';

// 生成随机的AI生成图片URL
function generateMockImages(count: number, prompt: string, style: string, size: string) {
  const images = [];
  const imageSize = size === '1:1' ? '512x512' : 
                   size === '16:9' ? '800x450' : 
                   size === '9:16' ? '450x800' : 
                   '640x480'; // 4:3
  
  const styleKeywords = {
    'realistic': 'photo',
    'anime': 'anime',
    'oil': 'oil painting',
    'watercolor': 'watercolor',
    'sketch': 'sketch',
    'digital': 'digital art'
  };
  
  const styleKeyword = styleKeywords[style] || 'photo';
  
  for (let i = 0; i < count; i++) {
    // 使用Faker生成随机图片，添加一些参数使图片看起来不同
    const imageUrl = `https://picsum.photos/${imageSize}?random=${faker.number.int({ min: 1, max: 1000 })}&prompt=${encodeURIComponent(prompt)}&style=${styleKeyword}`;
    images.push(imageUrl);
  }
  
  return images;
}

export default eventHandler(async (event) => {
  const userinfo = verifyAccessToken(event);
  if (!userinfo) {
    return unAuthorizedResponse(event);
  }
  
  // 获取请求体
  const body = await readBody(event);
  const { prompt, size = '1:1', style = 'realistic', creativity = 75 } = body;
  
  // 验证必要参数
  if (!prompt || prompt.trim() === '') {
    return createError({
      statusCode: 400,
      message: '请提供文字描述'
    });
  }
  
  // 模拟处理延迟
  await new Promise(resolve => setTimeout(resolve, 2000));
  
  // 生成随机图片数量（基于创意度）
  const imageCount = creativity > 80 ? 4 : creativity > 50 ? 3 : 2;
  
  // 生成模拟图片
  const images = generateMockImages(imageCount, prompt, style, size);
  
  // 返回成功响应
  return useResponseSuccess({
    images,
    prompt,
    style,
    size,
    creativity,
    generationId: faker.string.uuid(),
    generatedAt: new Date().toISOString()
  });
}); 